package com.cicadasmall.logger.aspect;

import com.cicadasmall.common.utils.SpringContextUtils;
import com.cicadasmall.data.domain.LogDO;
import com.cicadasmall.logger.annotation.ApiLog;
import com.cicadasmall.logger.constant.LogTypeEnum;
import com.cicadasmall.logger.event.LogEvent;
import com.cicadasmall.logger.utils.LogUtils;
import lombok.extern.slf4j.Slf4j;;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * LogAspect
 *
 * @author Jin
 */
@Slf4j
@Component
@Aspect
public class LogAspect {

    @Around("@annotation(apiLog)")
    public Object around(ProceedingJoinPoint point, ApiLog apiLog) throws Throwable {
        String strClassName = point.getTarget().getClass().getName();
        String strMethodName = point.getSignature().getName();
        log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName);
        LogDO logDO = LogUtils.buildSysLog(LogTypeEnum.操作日志);
        logDO.setTitle(apiLog.value());
        Long startTime = System.currentTimeMillis();
        Object obj = point.proceed();
        Long endTime = System.currentTimeMillis();
        logDO.setProcessTime(String.valueOf(endTime - startTime));
        SpringContextUtils.publishEvent(new LogEvent(logDO));
        return obj;
    }

}
